package com.tang.oss.utils;

import com.tang.oss.entity.FileInfo;
import io.minio.*;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MinioUtil {
    @Resource
    private MinioClient minioClient;

    @Value("${minio.url}")
    private String ip;

    @Value("${minio.domain}")
    private String domain;


    /**
     * 新建桶（若存在则不建立）
     *
     * @param bucketName 桶名
     * @author Tang
     * @date 2024/2/8 16:26
     **/
    public void createBucket(String bucketName) throws Exception {
        boolean bucketExists = minioClient.bucketExists(BucketExistsArgs.builder()
                .bucket(bucketName)
                .build());
        if (bucketExists) {
            return;
        }
        minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
    }

    /**
     * 上传文件
     *
     * @param inputStream 待上传文件的文件流
     * @param bucketName  桶名
     * @param objectName  文件名
     * @author Tang
     * @date 2024/2/8 16:29
     **/
    public void uploadFile(InputStream inputStream, String bucketName, String objectName) throws Exception {
        minioClient.putObject(PutObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .stream(inputStream, -1L, 5242889L)
                .build());
    }

    /**
     * 列出所有的桶
     *
     * @return List<String>
     * @author Tang
     * @date 2024/2/8 16:32
     **/
    public List<String> getAllBuckets() throws Exception {
        List<Bucket> buckets = minioClient.listBuckets();
        return buckets.stream().map(Bucket::name).collect(Collectors.toList());
    }

    /**
     * 列出当前桶及文件
     *
     * @param bucket 桶名
     * @return java.util.List<java.lang.String>
     * @author Tang
     * @date 2024/2/8 16:33
     **/
    public List<FileInfo> getAllFiles(String bucket) throws Exception {
        Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder()
                .bucket(bucket)
                .build());
        List<FileInfo> fileInfoList = new ArrayList<>();
        for (Result<Item> result : results) {
            Item item = result.get();
            FileInfo fileInfo = new FileInfo();
            fileInfo.setFileName(item.objectName());
            fileInfo.setDirectory(item.isDir());
            fileInfo.setEtag(item.etag());
        }
        return fileInfoList;
    }

    /**
     * 下载文件
     *
     * @param bucket     桶名
     * @param objectName 对象名
     * @return java.io.InputStream
     * @author Tang
     * @date 2024/2/8 16:40
     **/
    public InputStream download(String bucket, String objectName) throws Exception {
        return minioClient.getObject(GetObjectArgs.builder()
                .bucket(bucket)
                .object(objectName)
                .build());
    }

    /**
     * 删除桶
     *
     * @param bucket 桶名
     * @author Tang
     * @date 2024/2/8 16:40
     **/
    public void deleteBucket(String bucket) throws Exception {
        minioClient.removeBucket(RemoveBucketArgs.builder()
                .bucket(bucket)
                .build());
    }

    /**
     * 删除对象
     *
     * @param bucket     桶
     * @param objectName 对象名
     * @author Tang
     * @date 2024/2/8 16:41
     **/
    public void deleteObject(String bucket, String objectName) throws Exception {
        minioClient.removeObject(RemoveObjectArgs.builder()
                .bucket(bucket)
                .object(objectName).build());
    }

    /**
     * 获取文件访问地址
     *
     * @param bucketName 桶名
     * @param fileName   对象名
     * @return java.lang.String
     * @author Tang
     * @date 2024/2/18 14:31
     **/
    public String getPreviewFileUrl(String bucketName, String fileName) throws Exception {
        return String.join("/",domain,bucketName,fileName);
    }
}
